﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using System.IO;

using SGP;



namespace AnimationEditor
{
    public partial class Form1 : Form
    {
        string fileNameforSave;
        public string FileNameforSave
        {
            get { return fileNameforSave; }
            set { fileNameforSave = value; }
        }
        DateTime timer;
        public DateTime Timer
        {
            get { return timer; }
            set { timer = value; }
        }
        bool isPlaying;
        public bool IsPlaying
        {
            get { return isPlaying; }
            set { isPlaying = value; }
        }
        bool looping;
        public bool Looping
        {
            get { return looping; }
            set { looping = value; }
        }
        bool leftMouseIsDown;
        public bool LeftMouseIsDown
        {
            get { return leftMouseIsDown; }
            set { leftMouseIsDown = value; }
        }
        bool pickingAnchorPoint;
        public bool PickingAnchorPoint
        {
            get { return pickingAnchorPoint; }
            set { pickingAnchorPoint = value; }
        }
        bool drawingRectNow;
        public bool DrawingRectNow
        {
            get { return drawingRectNow; }
            set { drawingRectNow = value; }
        }
        int originalHeight;
        public int OriginalHeight
        {
            get { return originalHeight; }
            set { originalHeight = value; }
        }
        int frameCount;
        public int FrameCount
        {
            get { return frameCount; }
            set { frameCount = value; }
        }
        int panelImageID;
        public int PanelImageID
        {
            get { return panelImageID; }
            set { panelImageID = value; }
        }
        int panel2ImageID;
        public int Panel2ImageID
        {
            get { return panel2ImageID; }
            set { panel2ImageID = value; }
        }
        int panelXScaling;
        public int PanelXScaling
        {
            get { return panelXScaling; }
            set { panelXScaling = value; }
        }
        int panelYScaling;
        public int PanelYScaling
        {
            get { return panelYScaling; }
            set { panelYScaling = value; }
        }
        Point offset;
        public Point Offset
        {
            get { return offset; }
            set { offset = value; }
        }



        List<Frames> frameList;
        List<Animations> listofanimations;
        CSGP_Direct3D D3d = CSGP_Direct3D.GetInstance();
        CSGP_TextureManager TM = CSGP_TextureManager.GetInstance();
        Rectangle testRect;
        Rectangle drawingRect;
        Rectangle panel2Rect;
        Point AnchorPoint = new Point(-5, -5);


        public Form1()
        {
            InitializeComponent();
            leftMouseIsDown = false;
            pickingAnchorPoint = false;
            drawingRectNow = false;
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            panel2.Visible = false;
            label7.Visible = false;
            NUDDuration.Visible = false;
            label8.Visible = false;
            EventNameBox.Visible = false;
            APX.Enabled = false;
            APY.Enabled = false;
            MouseTestX.Enabled = false;
            MouseTestY.Enabled = false;
            MouseTestWidth.Enabled = false;
            MouseTestHeight.Enabled = false;
            toolStripStatusLabel1.Text = "";
            originalHeight = this.Height;
            //485
            this.Size = new Size(groupBox2.Left - 50, panel1.Location.X + panel1.Height + 100);
        }
        public void Init()
        {
            D3d.Initialize(panel1, false);
            D3d.AddRenderTarget(panel2);
            TM.Initialize(D3d.Device, D3d.Sprite);
            testRect = new Rectangle();
            drawingRect = new Rectangle();
            panel2Rect = new Rectangle();
            looping = true;
            isPlaying = false;
            frameCount = 0;
            panelImageID = -1;
            panel2ImageID = -1;
            panelXScaling = 1;
            panelYScaling = 1;
            frameList = new List<Frames>();
            listofanimations = new List<Animations>();
        }
        public new void Update()
        {
            if (isPlaying == true)
            {
                if (FramesBox.SelectedIndex == -1)
                    return;

                Frames f = (Frames)FramesBox.Items[FramesBox.SelectedIndex];
                DateTime temp = DateTime.Now;
                if (temp - timer >= TimeSpan.FromSeconds(f.Duration))
                {
                    if (FramesBox.SelectedIndex + 1 >= FramesBox.Items.Count)
                    {
                        Animations tempAnim = (Animations)AnimationsBox.Items[AnimationsBox.SelectedIndex];
                        if (tempAnim.IsLooping == 1)
                        {
                            FramesBox.SelectedIndex = 0;
                        }
                        else
                            isPlaying = false;
                    }
                    else
                        FramesBox.SelectedIndex += 1;

                    timer = DateTime.Now;
                }
            }
        }
        public void Render()
        {
            //Panel 1
            offset = new Point(0, 0);
            offset.X += panel1.AutoScrollPosition.X;
            offset.Y += panel1.AutoScrollPosition.Y;
            Point offset2 = new Point(panel2.Location.X, panel2.Location.Y);
            offset2.X += panel2.AutoScrollPosition.X;
            offset2.Y += panel2.AutoScrollPosition.Y;
            D3d.Clear(panel1, Color.Azure);
            D3d.DeviceBegin();
            D3d.SpriteBegin();

            if (panelImageID != -1)
            {
                panel1.AutoScrollMinSize = new Size(TM.GetTextureWidth(panelImageID), TM.GetTextureHeight(panelImageID));
                TM.Draw(panelImageID, offset.X, offset.Y, panelXScaling, panelYScaling);
            }

            //render Rect drawing
            if (!testRect.IsEmpty)
            {
                D3d.DrawHollowRect(new Rectangle(testRect.X + offset.X, testRect.Y + offset.Y, testRect.Width, testRect.Height), Color.Red, 2);
            }
            //Anchor Point drawing
            if (AnchorPoint.X != -5)
            {
                D3d.DrawRect(new Rectangle(new Point(((int)AnchorPoint.X + offset.X), (int)AnchorPoint.Y + offset.Y), new Size(3,3)), Color.Cyan);
            }
            D3d.SpriteEnd();
            D3d.DeviceEnd();
            D3d.Present();

            //Panel 2
            D3d.Clear(panel2, Color.Azure);
            D3d.DeviceBegin();
            D3d.SpriteBegin();

            if (panel2ImageID != -1 && FramesBox.SelectedIndex != -1)
            {
                //200x183
                int tempAPX = panel2.Width / 2;
                int tempAPY = panel2.Height / 2;
                Frames fTest = (Frames)frameList[FramesBox.SelectedIndex];
                int rectXminusAPX = fTest.APX - fTest.RectX;
                int rectYminusAPY = fTest.APY- fTest.RectY;

                panel2Rect = new Rectangle(tempAPX-rectXminusAPX, tempAPY-rectYminusAPY, 198, 180);
                Rectangle srcRect = Rectangle.Empty;
                srcRect = new Rectangle(fTest.RectX, fTest.RectY, fTest.RectWidth, fTest.RectHeight); ;
                TM.Draw(panel2ImageID, panel2Rect.X, panel2Rect.Y, panelXScaling, panelYScaling, srcRect);
            }
            D3d.SpriteEnd();
            D3d.DeviceEnd();
            D3d.Present();
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            Looping = false;
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Tacwars\\resource\\Animations");
            dlg.Filter = "All File|*.*|XmlFiles|*.xml";
            dlg.FilterIndex = 2;
            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement xRoot = XElement.Load(dlg.FileName);
                IEnumerable<XElement> xAnims = xRoot.Elements();

                foreach (XElement xAni in xAnims)
                {
                    Animations aniTest = new Animations();
                    XAttribute looping = xAni.Attribute("IsLooping");
                    int animLoop = int.Parse(looping.Value);
                    XAttribute framecount = xAni.Attribute("FrameCount");
                    int frameCs = int.Parse(framecount.Value);
                    aniTest.IsLooping = animLoop;
                    aniTest.FramesInAnimation = frameCs;
                    XAttribute picturePath = xAni.Attribute("FilePath");
                    string pP = Convert.ToString(picturePath.Value);
                    if (picturePath != null)
                    {
                        panelImageID = TM.LoadTexture(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Tacwars\\resource\\Images\\" + pP);
                        panel2ImageID = TM.LoadTexture(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Tacwars\\resource\\Images\\" + pP);
                        fileNameforSave = pP;
                    }

                    IEnumerable<XElement> xFrames = xAnims.Elements();
                    foreach (XElement xF in xFrames)
                    {
                        Frames frameTest = new Frames();
                        XElement xEvent = xF.Element("Frame");
                        XAttribute xEventName = xF.Attribute("EventName");
                        string pEN = Convert.ToString(xEventName.Value);
                        if (xEventName != null)
                        {
                            string fF = Convert.ToString(xEventName.Value);
                            frameTest.EventName = fF;
                        }

                        XAttribute frameNum = xF.Attribute("FrameNumber");
                        frameTest.FrameNumber = int.Parse(frameNum.Value);
                        XAttribute AnchorPX = xF.Attribute("AnchorPointX");
                        frameTest.APX = int.Parse(AnchorPX.Value);
                        XAttribute AnchorPY = xF.Attribute("AnchorPointY");
                        frameTest.APY = int.Parse(AnchorPY.Value);
                        XAttribute FDura = xF.Attribute("Time");
                        frameTest.Duration = float.Parse(FDura.Value);
                        XAttribute rRX = xF.Attribute("ACRX");
                        frameTest.RectX = int.Parse(rRX.Value);
                        XAttribute rRY = xF.Attribute("ACRY");
                        frameTest.RectY = int.Parse(rRY.Value);
                        XAttribute rRWidth = xF.Attribute("ACRWidth");
                        frameTest.RectWidth = int.Parse(rRWidth.Value);
                        XAttribute rRHeight = xF.Attribute("ACRHeight");
                        frameTest.RectHeight = int.Parse(rRHeight.Value);

                        aniTest.listofFrames.Add(frameTest);
                        frameList.Add(frameTest);

                    }
                    AnimationsBox.Items.Add(aniTest);
                    listofanimations.Add(aniTest);
                }
            }
            this.Size = new Size(groupBox2.Location.X + groupBox2.Width + 20, originalHeight);
            D3d.Resize(panel1, false);
            groupBox1.Visible = true;
            groupBox2.Visible = true;
            panel2.Visible = true;

        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Tacwars\\resource\\Animations");
            dlg.Filter = "All Files|*.*|Xml Files|*.xml";
            dlg.FilterIndex = 2;
            dlg.DefaultExt = "xml";

            if (DialogResult.OK == dlg.ShowDialog())
            {
                XElement XRoot = new XElement("Animations");
                for (int i = 0; i < AnimationsBox.Items.Count; i++)
                {
                    Animations xAni = (Animations)AnimationsBox.Items[i];
                    XElement xAnimation = new XElement("Animation");
                    XRoot.Add(xAnimation);
                    XAttribute xfilePath = new XAttribute("FilePath", fileNameforSave);
                    xAnimation.Add(xfilePath);
                    XAttribute xIsLooping = new XAttribute("IsLooping", xAni.IsLooping);
                    xAnimation.Add(xIsLooping);
                    XAttribute xFrameCount = new XAttribute("FrameCount", xAni.listofFrames.Count);
                    xAnimation.Add(xFrameCount);

                    for (int k = 0; k < xAni.listofFrames.Count; k++)
                    {
                        Frames xF = (Frames)xAni.listofFrames[k];
                        XElement xFrame = new XElement("Frame");
                        xAnimation.Add(xFrame);
                        XAttribute xEventName = new XAttribute("EventName", xF.EventName);
                        xFrame.Add(xEventName);
                        XAttribute xFrameNum = new XAttribute("FrameNumber", k);
                        xFrame.Add(xFrameNum);
                        XAttribute xAPX = new XAttribute("AnchorPointX", xF.AnchorPoint.X);
                        xFrame.Add(xAPX);
                        XAttribute xAPY = new XAttribute("AnchorPointY", xF.AnchorPoint.Y);
                        xFrame.Add(xAPY);
                        XAttribute xFDuration = new XAttribute("Time", xF.Duration);
                        xFrame.Add(xFDuration);
                        XAttribute xIRectX = new XAttribute("iRectX", 0);
                        xFrame.Add(xIRectX);
                        XAttribute xIRectY = new XAttribute("iRectY", 0);
                        xFrame.Add(xIRectY);
                        XAttribute xIRectWidth = new XAttribute("iRectWidth", TM.GetTextureWidth(panelImageID));
                        xFrame.Add(xIRectWidth);
                        XAttribute xIRectHeight = new XAttribute("iRectHeight", TM.GetTextureHeight(panelImageID));
                        xFrame.Add(xIRectHeight);
                        XAttribute xActiveRectX = new XAttribute("ACRX", xF.RectX);
                        xFrame.Add(xActiveRectX);
                        XAttribute xActiveRectY = new XAttribute("ACRY", xF.RectY);
                        xFrame.Add(xActiveRectY);
                        XAttribute xActiveRectWidth = new XAttribute("ACRWidth", xF.RectWidth);
                        xFrame.Add(xActiveRectWidth);
                        XAttribute xActiveRectHeight = new XAttribute("ACRHeight", xF.RectHeight);
                        xFrame.Add(xActiveRectHeight);
                    }
                }
                XRoot.Save(dlg.FileName);
            }
        }
        //Exit, alt+f4
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //Load File...
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.InitialDirectory = System.IO.Path.GetFullPath(System.IO.Directory.GetCurrentDirectory() + "\\..\\..\\Tacwars\\resource\\Images");
            if (DialogResult.OK == dlg.ShowDialog())
            {
                Size asdf = panel1.Size;
                panelImageID = TM.LoadTexture(dlg.FileName);
                panel2ImageID = TM.LoadTexture(dlg.FileName);

                fileNameforSave = Path.GetFileName(dlg.FileName);
                this.Size = new Size(groupBox2.Location.X + groupBox2.Width + 20, originalHeight);
                groupBox1.Visible = true;
                groupBox2.Visible = true;
                panel2.Visible = true;

            }
        }

        //When I click and hold down on the panel
        private void panel1_MouseClick(object sender, MouseEventArgs e)
        {
            offset = new Point(0, 0);
            offset.X += panel1.AutoScrollPosition.X;
            offset.Y += panel1.AutoScrollPosition.Y;

            if (e.Button == MouseButtons.Left)
            {
                if (drawingRectNow == true)
                {
                    leftMouseIsDown = true;
                    if (panel1.ClientRectangle.Contains(e.Location))
                    {
                        MouseTestX.Value = 0;
                        MouseTestY.Value = 0;
                        MouseTestWidth.Value = 0;
                        MouseTestHeight.Value = 0;
                        testRect = Rectangle.Empty;
                        drawingRect = Rectangle.Empty;
                        MouseTestX.Value = e.Location.X - offset.X;
                        MouseTestY.Value = e.Location.Y - offset.Y;
                        testRect = new Rectangle(new Point(e.Location.X, e.Location.Y), new Size(0, 0));
                    }
                }

                if (pickingAnchorPoint == true)
                {
                    leftMouseIsDown = true;
                    if (panel1.ClientRectangle.Contains(e.Location))
                    {
                        AnchorPoint.X = e.Location.X - offset.X;
                        AnchorPoint.Y = e.Location.Y - offset.Y;
                        APX.Value = AnchorPoint.X;
                        APY.Value = AnchorPoint.Y;
                    }
                }
            }
        }

        //Mouse moving while the mouse is held down
        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            offset = new Point(0, 0);
            offset.X += panel1.AutoScrollPosition.X;
            offset.Y += panel1.AutoScrollPosition.Y;
            //if (drawingRectNow == false && pickingAnchorPoint == false)
            {
                Point offset3 = e.Location;
                offset3.X = e.Location.X;
                offset3.Y = e.Location.Y;
                offset3.X -= panel1.AutoScrollPosition.X;
                offset3.Y -= panel1.AutoScrollPosition.Y;
                toolStripStatusLabel1.Text = offset3.ToString();
            }

            if (drawingRectNow == true && leftMouseIsDown == true)
            {
                decimal testWidth = ((e.Location.X / panelXScaling) - testRect.X);
                decimal testHeight = ((e.Location.Y / panelYScaling) - testRect.Y);
                testRect.Width = (int)testWidth;
                testRect.Height = (int)testHeight;
                MouseTestWidth.Value = Math.Abs(testRect.Width);
                MouseTestHeight.Value = Math.Abs(testRect.Height);

                if (e.Location.X <= 0)
                    MouseTestX.Value = 0;
                else
                {
                    if (e.Location.X < testRect.X)
                        MouseTestX.Value = e.Location.X - offset.X;
                    else
                        MouseTestX.Value = testRect.X;
                }

                if (e.Location.Y <= 0)
                    MouseTestY.Value = 0;
                else
                {
                    if (e.Location.Y < testRect.Y)
                        MouseTestY.Value = e.Location.Y - offset.Y;
                    else
                        MouseTestY.Value = testRect.Y;
                }
            }

            if (pickingAnchorPoint == true && leftMouseIsDown == true)
            {
                AnchorPoint.X = e.Location.X - offset.X;
                AnchorPoint.Y = e.Location.Y - offset.Y;
                APX.Value = AnchorPoint.X;
                APY.Value = AnchorPoint.Y;
            }
        }

        //Anchor Point
        private void button2_Click(object sender, EventArgs e)
        {
            // toolStripStatusLabel1.Text = "Click on the image to set your Anchor Point";
            pickingAnchorPoint = true;
            APX.Enabled = true;
            APY.Enabled = true;
            MouseTestX.Enabled = false;
            MouseTestY.Enabled = false;
            MouseTestWidth.Enabled = false;
            MouseTestHeight.Enabled = false;
            panel1.Cursor = Cursors.Hand;
        }

        //Render Rect
        private void button3_Click(object sender, EventArgs e)
        {
            //toolStripStatusLabel1.Text = "Click and drag the mouse to make your Frame's image rect";
            drawingRectNow = true;
            APX.Enabled = false;
            APY.Enabled = false;
            MouseTestX.Enabled = true;
            MouseTestY.Enabled = true;
            MouseTestWidth.Enabled = true;
            MouseTestHeight.Enabled = true;
            panel1.Cursor = Cursors.Cross;
        }

        //Mouse is up after setting AP or rect
        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            if (drawingRectNow == true && leftMouseIsDown == true)
            {
                leftMouseIsDown = false;
                drawingRectNow = false;
                panel1.Cursor = Cursors.Default;
                testRect.Width = (int)(e.Location.X - testRect.X);
                testRect.Height = (int)(e.Location.Y - testRect.Y);

                if (testRect.X < e.Location.X)
                {
                    if (e.Location.X <= 0)
                    {
                        testRect.Width = Math.Abs(testRect.Width - e.Location.X);
                        testRect.X = 0;
                        MouseTestX.Value = 0;
                    }
                    else
                        MouseTestX.Value = testRect.Left / panelXScaling;


                }

                if (testRect.Y < e.Location.Y)
                {
                    if (e.Location.Y <= 0)
                    {
                        testRect.Height = Math.Abs(testRect.Height - e.Location.Y);
                        AnchorPoint.Y = testRect.Bottom;
                        testRect.Y = 0;
                        MouseTestY.Value = 0;
                    }
                    else
                        MouseTestY.Value = testRect.Top;
                }
            }

            if (pickingAnchorPoint == true && leftMouseIsDown == true)
            {
                leftMouseIsDown = false;
                pickingAnchorPoint = false;
                panel1.Cursor = Cursors.Default;
                AnchorPoint.X = e.Location.X - offset.X;
                AnchorPoint.Y = e.Location.Y - offset.Y;
                APX.Value = AnchorPoint.X;
                APY.Value = AnchorPoint.Y;
            }
        }

        //Add current stats to a frame in the listbox
        private void button4_Click(object sender, EventArgs e)
        {
            if (AnchorPoint.X > 0)
            {
                Frames testFrame = new Frames();
                testRect = Rectangle.Empty;
                FramesBox.SelectedIndex = -1;
                testFrame.Duration = 0.5f;

                //Adding the frame to the listbox and internal list of frames
                testFrame.FrameNumber = frameCount;
                testFrame.EventName = "";
                testFrame.APX = (int)APX.Value;
                testFrame.APY = (int)APY.Value;
                testFrame.RectX = (int)MouseTestX.Value;
                testFrame.RectY = (int)MouseTestY.Value;
                testFrame.RectWidth = (int)MouseTestWidth.Value;
                testFrame.RectHeight = (int)MouseTestHeight.Value;
                frameList.Add(testFrame);
                FramesBox.Items.Add(testFrame);
                frameCount += 1;

                //Resetting all stats
                MouseTestX.Value = 0;
                MouseTestY.Value = 0;
                MouseTestWidth.Value = 0;
                MouseTestHeight.Value = 0;
                APX.Value = 0;
                APY.Value = 0;
                AnchorPoint.X = -5;
                AnchorPoint.Y = -5;
            }
            APX.Enabled = false;
            APY.Enabled = false;
            MouseTestX.Enabled = false;
            MouseTestY.Enabled = false;
            MouseTestWidth.Enabled = false;
            MouseTestHeight.Enabled = false;
        }

        //FramesBox index chosen
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (FramesBox.SelectedIndex == -1)
                return;

            label7.Visible = true;
            NUDDuration.Visible = true;
            label8.Visible = true;
            EventNameBox.Visible = true;

            Frames f = (Frames)frameList[FramesBox.SelectedIndex];
            APX.Value = f.AnchorPoint.X;
            APY.Value = f.AnchorPoint.Y;
            MouseTestX.Value = f.RectX;
            MouseTestY.Value = f.RectY;
            MouseTestWidth.Value = f.RectWidth;
            MouseTestHeight.Value = f.RectHeight;
            NUDDuration.Value = (decimal)f.Duration;
            EventNameBox.Text = f.EventName;

            panel2Rect.Location = new Point((int)MouseTestX.Value, (int)MouseTestY.Value);
            panel2Rect.Size = new Size(f.RectWidth, f.RectHeight);

            testRect.X = (int)MouseTestX.Value;
            testRect.Y = (int)MouseTestY.Value;
            testRect.Width = f.RectWidth;
            testRect.Height = f.RectHeight;
            AnchorPoint.X = f.AnchorPoint.X;
            AnchorPoint.Y = f.AnchorPoint.Y;

        }

        //Putting all frames into one animation
        private void FtoAButton_Click(object sender, EventArgs e)
        {
            if (FramesBox.Items.Count < 1)
                return;

            Animations anim = new Animations();

            for (int i = 0; i < FramesBox.Items.Count; i++)
            {
                Frames f = (Frames)FramesBox.Items[i];
                anim.listofFrames.Add(f);
            }
            anim.IsLooping = 1;
            listofanimations.Add(anim);
            AnimationsBox.Items.Add(anim);

            label7.Visible = false;
            NUDDuration.Visible = false;
            EventNameBox.Text = null;

            FramesBox.Items.Clear();
            frameCount = 0;
        }

        //When the APX NUD changes
        private void APX_ValueChanged(object sender, EventArgs e)
        {
              AnchorPoint.X = (int)APX.Value;
        }

        //When the APY NUD changes
        private void APY_ValueChanged(object sender, EventArgs e)
        {
            AnchorPoint.Y = (int)APY.Value;
        }

        //When the rect's X NUD changes
        private void MouseTestX_ValueChanged(object sender, EventArgs e)
        {
            if (leftMouseIsDown == false)
            {
                testRect.X = (int)MouseTestX.Value;
            }
        }

        //When the rect's Y NUD changes
        private void MouseTestY_ValueChanged(object sender, EventArgs e)
        {
            if (leftMouseIsDown == false)
            {
                testRect.Y = (int)MouseTestY.Value;
            }
        }

        //When the rect's Width NUD changes
        private void MouseTestWidth_ValueChanged(object sender, EventArgs e)
        {
            if (leftMouseIsDown == false)
            {
                testRect.Width = (int)MouseTestWidth.Value;
            }

        }

        //When the rect's Height NUD changes
        private void MouseTestHeight_ValueChanged(object sender, EventArgs e)
        {
            if (leftMouseIsDown == false)
            {
                testRect.Height = (int)MouseTestHeight.Value;
            }
        }

        //Zoom in
        private void button5_Click(object sender, EventArgs e)
        {
            if (panelXScaling < 10)
            {
                panelXScaling += 1;
                panelYScaling += 1;
                testRect.X = (int)MouseTestX.Value * panelXScaling;
                testRect.Y = (int)MouseTestY.Value * panelYScaling;
                testRect.Width = (int)MouseTestWidth.Value * panelXScaling;
                testRect.Height = (int)MouseTestHeight.Value * panelYScaling;

            }
            //if (panelImageID != -1)
            //{
            //    int testX = TM.GetTextureWidth(panelImageID) * (int)panelXScaling;
            //    int testY = TM.GetTextureHeight(panelImageID) * (int)panelYScaling;
            //    panel1.AutoScrollMinSize = new Size(testX * panelXScaling, testY * panelYScaling);
            //}      
        }

        //Zoom out
        private void button6_Click(object sender, EventArgs e)
        {
            if (panelYScaling > 1)
            {
                panelXScaling -= 1;
                panelYScaling -= 1;
                testRect.X = (int)MouseTestX.Value * panelXScaling;
                testRect.Y = (int)MouseTestY.Value * panelYScaling;
                testRect.Width = (int)MouseTestWidth.Value * panelXScaling;
                testRect.Height = (int)MouseTestHeight.Value * panelYScaling;
            }
            //if (panelImageID != -1)
            //{
            //    int testX = TM.GetTextureWidth(panelImageID) * (int)panelXScaling;
            //    int testY = TM.GetTextureHeight(panelImageID) * (int)panelYScaling;
            //    panel1.AutoScrollMinSize = new Size(testX * panelXScaling, testY * panelYScaling);
            //}      
        }

        //When the AnimationsBox index changes
        private void AnimationsBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (AnimationsBox.SelectedIndex == -1)
                return;

            FramesBox.Items.Clear();
            Animations ani = (Animations)AnimationsBox.Items[AnimationsBox.SelectedIndex];
            for (int i = 0; i < ani.listofFrames.Count; i++)
            {
                FramesBox.Items.Add(ani.listofFrames[i]);
            }
        }

        //Play
        private void button7_Click(object sender, EventArgs e)
        {
            if (AnimationsBox.SelectedIndex == -1)
                return;

            FramesBox.SelectedIndex = 0;
            isPlaying = true;
            timer = DateTime.Now;
        }

        //If I click a part of the Frames listbox that isn't an index, deselect
        private void FramesBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y > FramesBox.ItemHeight * FramesBox.Items.Count)
            {
                FramesBox.SelectedItems.Clear();
                testRect = Rectangle.Empty;
                MouseTestX.Value = 0;
                MouseTestY.Value = 0;
                MouseTestWidth.Value = 0;
                MouseTestHeight.Value = 0;
                APX.Value = 0;
                APY.Value = 0;
                AnchorPoint.X = -5;
                AnchorPoint.Y = -5;
                label7.Visible = false;
                NUDDuration.Visible = false;
                EventNameBox.Visible = false;
                label8.Visible = false;
            }
        }

        //If I click a part of the Animations listbox that isn't an index, deselect
        private void AnimationsBox_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Y > AnimationsBox.ItemHeight * AnimationsBox.Items.Count)
            {
                FramesBox.Items.Clear();
                AnimationsBox.SelectedItems.Clear();
                label7.Visible = false;
                NUDDuration.Visible = false;
                EventNameBox.Visible = false;
                label8.Visible = false;
            }
        }

        //Stop
        private void button8_Click(object sender, EventArgs e)
        {
            isPlaying = false;
        }

        //Pause/Unpause
        private void button9_Click(object sender, EventArgs e)
        {
            isPlaying = !isPlaying;
        }

        //Clear
        private void button10_Click(object sender, EventArgs e)
        {
            AnimationsBox.SelectedItems.Clear();
            FramesBox.SelectedItems.Clear();
            testRect = Rectangle.Empty;
            AnchorPoint.X = -5;
            AnchorPoint.Y = -5;
            label7.Visible = false;
            NUDDuration.Visible = false;
            EventNameBox.Visible = false;
            label8.Visible = false;

            MouseTestX.Value = 0;
            MouseTestY.Value = 0;
            MouseTestWidth.Value = 0;
            MouseTestHeight.Value = 0;
            APX.Value = 0;
            APY.Value = 0;
        }

        //When the duration NUD changes
        private void NUDDuration_ValueChanged(object sender, EventArgs e)
        {
            Frames f = (Frames)FramesBox.Items[FramesBox.SelectedIndex];
            f.Duration = (float)NUDDuration.Value;
        }

        //Update frame buttons
        private void button11_Click(object sender, EventArgs e)
        {
            if (FramesBox.SelectedIndex == -1)
                return;

            Frames f = (Frames)FramesBox.Items[FramesBox.SelectedIndex];
            Frames lf = (Frames)frameList[FramesBox.SelectedIndex];
            Frames updatedF = new Frames();

            updatedF.FrameNumber = f.FrameNumber;

            if (EventNameBox.Text != "")
                updatedF.EventName = EventNameBox.Text;
            else
                updatedF.EventName = "";

            updatedF.Duration = f.Duration;
            updatedF.APX = (int)APX.Value;
            updatedF.APY = (int)APY.Value;
            updatedF.RectX = (int)MouseTestX.Value;
            updatedF.RectY = (int)MouseTestY.Value;
            updatedF.RectWidth = (int)MouseTestWidth.Value;
            updatedF.RectHeight = (int)MouseTestHeight.Value;

            int temp = FramesBox.SelectedIndex;
            frameList.Insert(FramesBox.SelectedIndex, updatedF);
            FramesBox.Items.Insert(FramesBox.SelectedIndex, updatedF);
            EventNameBox.Text = "";
            MouseTestX.Value = 0;
            MouseTestY.Value = 0;
            MouseTestWidth.Value = 0;
            MouseTestHeight.Value = 0;
            APX.Value = 0;
            APY.Value = 0;
            AnchorPoint.X = -5;
            AnchorPoint.Y = -5;
            frameList.Remove(lf);
            FramesBox.Items.Remove(f);
            FramesBox.SelectedIndex = temp;
        }

        //Remove Animation
        private void button12_Click(object sender, EventArgs e)
        {
            Animations a = (Animations)AnimationsBox.Items[AnimationsBox.SelectedIndex];
            AnimationsBox.Items.Remove(a);
            listofanimations.Remove(a);
            FramesBox.Items.Clear();
            button10_Click(sender, e);
        }

        //Remove Frame
        private void button13_Click(object sender, EventArgs e)
        {
            if (FramesBox.SelectedIndex == -1)
                return;
            Frames f = (Frames)FramesBox.Items[FramesBox.SelectedIndex];
            FramesBox.Items.Remove(f);
            frameList.Remove(f);
            button10_Click(sender, e);
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FramesBox.Items.Clear();
            AnimationsBox.Items.Clear();
            button10_Click(sender, e);
            if (panelImageID != -1)
                TM.UnloadTexture(panelImageID);
            if (panel2ImageID != -1)
                TM.UnloadTexture(panel2ImageID);

            panelImageID = -1;
            panel2ImageID = -1;
            leftMouseIsDown = false;
            pickingAnchorPoint = false;
            drawingRectNow = false;
            groupBox1.Visible = false;
            groupBox2.Visible = false;
            panel2.Visible = false;
            label7.Visible = false;
            NUDDuration.Visible = false;
            label8.Visible = false;
            EventNameBox.Visible = false;
            toolStripStatusLabel1.Text = "";
            originalHeight = this.Height;
            //485
            this.Size = new Size(groupBox2.Location.X - 50, panel1.Location.X + panel1.Height + 100);
            testRect = new Rectangle();
            drawingRect = new Rectangle();
            panel2Rect = new Rectangle();
            looping = true;
            isPlaying = false;
            frameCount = 0;
            panelImageID = -1;
            panel2ImageID = -1;

            frameList.Clear();
            listofanimations.Clear();
        }

        private void EventNameBox_TextChanged(object sender, EventArgs e)
        {
            Frames f = (Frames)FramesBox.Items[FramesBox.SelectedIndex];
            f.EventName = EventNameBox.Text;
        }
    }
}
